1
|
|
|
|
2
|
|
|
/* jshint -W101, -W098 */ |
3
|
|
|
|
4
|
|
|
var bip70 = require('../main.js'); |
5
|
|
|
var assert = require('assert'); |
6
|
|
|
var ProtoBuf = bip70.ProtoBuf; |
7
|
|
|
var PaymentDetails = ProtoBuf.PaymentDetails; |
8
|
|
|
var PaymentRequest = ProtoBuf.PaymentRequest; |
9
|
|
|
var X509Certificates = ProtoBuf.X509Certificates; |
10
|
|
|
var Output = ProtoBuf.Output; |
11
|
|
|
|
12
|
|
|
describe('Protobuf', function() { |
13
|
|
|
describe('Output', function() { |
14
|
|
|
[ |
15
|
|
|
[0, new Buffer("")], |
16
|
|
|
[1234567890, new Buffer("ascii")] |
17
|
|
|
].map(function(fixture, i) { |
18
|
|
|
it("fixture #" + i + " can be encoded and decoded", function(cb) { |
19
|
|
|
var amount = fixture[0]; |
20
|
|
|
var script = fixture[1]; |
21
|
|
|
var output = Output.create({ |
22
|
|
|
amount: amount, |
23
|
|
|
script: script |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
assert.equal(output.amount, amount); |
27
|
|
|
assert.ok(output.script.equals(script)); |
28
|
|
|
|
29
|
|
|
var encoded = new Buffer(Output.encode(output).finish()); |
30
|
|
|
var decoded = Output.decode(encoded); |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
assert.equal(decoded.amount, amount); |
34
|
|
|
assert.ok(decoded.script.equals(script)); |
35
|
|
|
|
36
|
|
|
cb(); |
37
|
|
|
}); |
38
|
|
|
}); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
describe('PaymentDetails', function() { |
42
|
|
|
it('set a create time', function(cb) { |
43
|
|
|
var time = 1513527325; |
44
|
|
|
|
45
|
|
|
var details = PaymentDetails.create({ |
46
|
|
|
time: time |
47
|
|
|
}); |
48
|
|
|
var str = PaymentDetails.encode(details).finish(); |
49
|
|
|
var decoded = PaymentDetails.decode(str); |
50
|
|
|
|
51
|
|
|
assert.equal(decoded.time, time); |
52
|
|
|
cb(); |
53
|
|
|
}); |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
describe('PaymentRequest', function() { |
57
|
|
|
it('parses a static PaymentRequest', function(cb) { |
58
|
|
|
var pkiType = "x509+sha256"; |
59
|
|
|
var memo = "Payment for 1 shoes"; |
60
|
|
|
var network = "main"; |
61
|
|
|
var paymentUrl = "https://example.com/payment"; |
62
|
|
|
var txOutVal = 500000; |
63
|
|
|
var merchantData = Buffer.from("30ae4a789834ed78ed74ff1291689131", "hex"); |
64
|
|
|
var txOutScript = Buffer.from("76a914ef137c53ddae4dcf04f5a656c42f451c0b99165788ac", "hex"); |
65
|
|
|
var someRequest = "Egt4NTA5K3NoYTI1NhqQCAqFBDCCAgEwggFqAgkAqnj+xtf3r5MwDQYJKoZIhvcNAQELBQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0xNTA5MjkxMzU2NDBaFw0xNjA5MjgxMzU2NDBaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBALKAUiA22Umgy666aJ9Ka1ilvU/JGfCMN/hmGKCR5kfnfOVaSdhm3ZCvnAwbUwS2j3DZ1jofRG3OV9PelRry8bSMb8zADtdGSVovjbTzlqkNzIS2ZwRglL05gkLPJnNJB/0M/1JNgCKeqA9hw0CMgR5B5ozFmR8OxplFLQDa3S2hAgMBAAEwDQYJKoZIhvcNAQELBQADgYEACwtR35RSKJG8sNYxgfCUwFKxPSxto6FQ9ge59xZ5xPOPLGuS4Otadf0hyKyrRGZGqVe8U8MEzi5Q32C0daB+llTX96winSkxy8T9t28AJLEJGG32qvLZzxkTn0LiwfH0obnCNxcXVlKsANIVKkZxTcd1g8PG5YuTyHNA6GL2rN4KhQQwggIBMIIBagIJAKp4/sbX96+TMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMTUwOTI5MTM1NjQwWhcNMTYwOTI4MTM1NjQwWjBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCygFIgNtlJoMuuumifSmtYpb1PyRnwjDf4ZhigkeZH53zlWknYZt2Qr5wMG1MEto9w2dY6H0RtzlfT3pUa8vG0jG/MwA7XRklaL42085apDcyEtmcEYJS9OYJCzyZzSQf9DP9STYAinqgPYcNAjIEeQeaMxZkfDsaZRS0A2t0toQIDAQABMA0GCSqGSIb3DQEBCwUAA4GBAAsLUd+UUiiRvLDWMYHwlMBSsT0sbaOhUPYHufcWecTzjyxrkuDrWnX9Icisq0RmRqlXvFPDBM4uUN9gtHWgfpZU1/esIp0pMcvE/bdvACSxCRht9qry2c8ZE59C4sHx9KG5wjcXF1ZSrADSFSpGcU3HdYPDxuWLk8hzQOhi9qzeInESHwigwh4SGXapFO8TfFPdrk3PBPWmVsQvRRwLmRZXiKwYpayozwUglZCozwUqE1BheW1lbnQgZm9yIDEgc2hvZXMyG2h0dHBzOi8vZXhhbXBsZS5jb20vcGF5bWVudDoQMK5KeJg07XjtdP8SkWiRMSqAARFwvKAuhNc3DiD6yk/SgD41uej9fflYbWvjRN4dD2xeQ/Z6We/H7gKdKPYzTynTj0osZnUcPq/An1opewevdjpPPYBwoTAa+ClYX3g4eMofJseLT/+60r0nS39xbbxxlUBdSmItqqoBEl853r8yBAfLA0aMGW47v1xeo62DI3lb"; |
66
|
|
|
var rawRequest = Buffer.from(someRequest, 'base64'); |
67
|
|
|
|
68
|
|
|
var paymentRequest = PaymentRequest.decode(rawRequest); |
69
|
|
|
assert.equal(paymentRequest.pkiType, pkiType); |
70
|
|
|
|
71
|
|
|
var paymentDetails = PaymentDetails.decode(paymentRequest.serializedPaymentDetails); |
72
|
|
|
assert.equal(paymentDetails.memo, memo); |
73
|
|
|
assert.equal(paymentDetails.network, network); |
74
|
|
|
assert.equal(paymentDetails.paymentUrl, paymentUrl); |
75
|
|
|
assert.equal(paymentDetails.outputs[0].amount, txOutVal); |
76
|
|
|
|
77
|
|
|
var script = Buffer.from(paymentDetails.outputs[0].script); |
78
|
|
|
assert.equal(script.toString('hex'), txOutScript.toString('hex')); |
79
|
|
|
|
80
|
|
|
var m = Buffer.from(paymentDetails.merchantData); |
81
|
|
|
assert.equal(m.toString('hex'), merchantData.toString('hex')); |
82
|
|
|
|
83
|
|
|
var x509 = X509Certificates.decode(paymentRequest.pkiData); |
84
|
|
|
assert.equal(x509.certificate.length, 2); |
85
|
|
|
|
86
|
|
|
cb(); |
87
|
|
|
}); |
88
|
|
|
}); |
89
|
|
|
}); |
90
|
|
|
|